package edu.lsnu.equipment_management_system.controller;

import edu.lsnu.equipment_management_system.exception.DateNullException;
import edu.lsnu.equipment_management_system.pojo.domain.JsonData;
import edu.lsnu.equipment_management_system.pojo.vo.in.EquipmentNoAndDateFromPage;
import edu.lsnu.equipment_management_system.pojo.vo.in.EquipmentNoListFromPage;
import edu.lsnu.equipment_management_system.service.AdminReviewService;
import edu.lsnu.equipment_management_system.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author mzy
 * @Description 管理员设备审核需要的
 * @date 2018/11/19
 */
@RestController
public class AdminReviewController {
    @Autowired
    AdminReviewService adminReviewService;

    // ------------------单个审核------------------
    /**
     * @Description 1.报损报废审核
     * @author mzy
     * @time 2018-11-26
     * @see
     * @return   java.lang.Object
     * @params  []
     */
    @GetMapping("/api/admin/damageReview/{equipmentNo}")
    public Object damageReview(@PathVariable String equipmentNo) {
        equipmentNo = equipmentNo.trim();
        boolean res =
                adminReviewService.damageReview(equipmentNo);

        if (res) {
            return JsonData.buildSuccess(0, "ok", "操作成功");
        } else {
            return JsonData.buildError("操作失败");
        }
    }

    /**
     * @Description 2.调入审核
     * @author mzy
     * @time 2018-11-26
     * @see
     * @return   java.lang.Object
     * @params  []
     */
    @GetMapping("/api/admin/transferInReview/{equipmentNo}")
    public Object transferInReview(@PathVariable String equipmentNo) {
        equipmentNo = equipmentNo.trim();
        boolean res =
                adminReviewService.transferInReview(equipmentNo);

        if (res) {
            return JsonData.buildSuccess(0, "ok", "操作成功");
        } else {
            return JsonData.buildError("操作失败");
        }
    }

    /**
     * @Description 3.调出审核
     * @author mzy
     * @time 2018-11-26
     * @see
     * @return   java.lang.Object
     * @params  []
     */
    @GetMapping("/api/admin/transferOutReview/{equipmentNo}")
    public Object transferOutReview(@PathVariable String equipmentNo) {
        equipmentNo = equipmentNo.trim();
        boolean res =
                adminReviewService.transferOutReview(equipmentNo);

        if (res) {
            return JsonData.buildSuccess(0, "ok", "操作成功");
        } else {
            return JsonData.buildError("操作失败");
        }
    }

    /**
     * @Description 4.购入审核
     * @author mzy
     * @time 2018-11-26
     * @see
     * @return   java.lang.Object
     * @params  []
     */
    @GetMapping("/api/admin/purchaseReview/{equipmentNo}")
    public Object purchaseReview(@PathVariable String equipmentNo) {
        equipmentNo = equipmentNo.trim();
        boolean res =
                adminReviewService.purchaseReview(equipmentNo);

        if (res) {
            return JsonData.buildSuccess(0, "ok", "操作成功");
        } else {
            return JsonData.buildError("操作失败");
        }
    }

    /**
     * @Description 5.设备使用审核
     * @author mzy
     * @time 2018-11-26
     * @see
     * @return   java.lang.Object
     * @params  []
     */
    @GetMapping("/api/admin/useRecordReview/{equipmentNo}/{beginDate}")
    public Object useRecordReview(@PathVariable String equipmentNo, @PathVariable String beginDate) {
        equipmentNo = equipmentNo.trim();
        beginDate = beginDate.trim();
        Date date = DateUtil.parseDate(beginDate);

        boolean res = adminReviewService.useRecordReview(equipmentNo, date);

        if (res) {
            return JsonData.buildSuccess(0, "ok" ,"操作成功");
        } else {
            return JsonData.buildError("操作失败");
        }
    }

    /**
     * @Description 6.设备维修维护审核
     * @author mzy
     * @time 2018-11-26
     * @see
     * @return   java.lang.Object
     * @params  []
     */
    @GetMapping("/api/admin/repairReview/{equipmentNo}/{beginDate}")
    public Object repairReview(@PathVariable String equipmentNo, @PathVariable String beginDate) {
        equipmentNo = equipmentNo.trim();
        beginDate = beginDate.trim();
        Date date = DateUtil.parseDate(beginDate);

        boolean res = adminReviewService.repairReview(equipmentNo, date);

        if (res) {
            return JsonData.buildSuccess(0, "ok", "操作成功");
        } else {
            return JsonData.buildError("操作失败");
        }
    }

    // ------------------多个审核------------------：多个审核能够容忍失败
    /**
     * @Description 1.批量报损报废审核
     * @author mzy
     * @time 2018-11-26
     * @see
     * @return   java.lang.Object
     * @params  []
     */
    @PostMapping("/api/admin/damageReview-post")
    /** 注意：之前是模拟POST表单提交所以直接不用@RequestBody可以，但是使用JSON转的话@RequestBody不要忘啦 */
    public Object damageReview(@RequestBody EquipmentNoListFromPage equipmentNoListFromPage) {

        // System.out.println(equipmentNoListFromPage);
        adminReviewService.damageReviewMore(equipmentNoListFromPage.getEquipmentNo());

        return JsonData.buildSuccess(0, "ok", "操作成功");
    }

    /**
     * @Description 2.批量调入审核
     * @author mzy
     * @time 2018-11-26
     * @see
     * @return   java.lang.Object
     * @params  []
     */
    @PostMapping("/api/admin/transferInReview-post")
    public Object transferInReview(@RequestBody EquipmentNoListFromPage equipmentNoListFromPage) {

        adminReviewService.transferInReviewMore(equipmentNoListFromPage.getEquipmentNo());

        return JsonData.buildSuccess(0, "ok", "操作成功");
    }

    /**
     * @Description 3.批量调出审核
     * @author mzy
     * @time 2018-11-26
     * @see
     * @return   java.lang.Object
     * @params  []
     */
    @PostMapping("/api/admin/transferOutReview-post")
    public Object transferOutReview(@RequestBody EquipmentNoListFromPage equipmentNoListFromPage) {

        adminReviewService.transferOutReviewMore(equipmentNoListFromPage.getEquipmentNo());

        return JsonData.buildSuccess(0, "ok", "操作成功");
    }

    /**
     * @Description 4.批量购入审核
     * @author mzy
     * @time 2018-11-26
     * @see
     * @return   java.lang.Object
     * @params  []
     */
    @PostMapping("/api/admin/purchaseReview-post")
    public Object purchaseReview(@RequestBody EquipmentNoListFromPage equipmentNoListFromPage) {

        adminReviewService.purchaseReviewMore(equipmentNoListFromPage.getEquipmentNo());

        return JsonData.buildSuccess(0, "ok", "操作成功");
    }

    /**
     * @Description 5.批量设备使用审核
     * @author mzy
     * @time 2018-11-26
     * @see
     * @return   java.lang.Object
     * @params  []
     */
    @PostMapping("/api/admin/useRecordReview-post")
    /** 注意：之前是模拟POST表单提交所以直接不用@RequestBody可以，但是使用JSON转的话@RequestBody不要忘啦 */
    public Object useRecordReview(@RequestBody EquipmentNoAndDateFromPage equipmentNoAndDateFromPage) {

        try {
            adminReviewService.useRecordReviewMore(equipmentNoAndDateFromPage.getEquipmentNoAndDate());
        } catch (DateNullException e) {
            return JsonData.buildError(e.getMessage());
        }

        return JsonData.buildSuccess(0, "ok" ,"操作成功");
    }

    /**
     * @Description 6.批量设备维修维护审核
     * @author mzy
     * @time 2018-11-26
     * @see
     * @return   java.lang.Object
     * @params  []
     */
    @PostMapping("/api/admin/repairReview-post")
    public Object repairReview(@RequestBody EquipmentNoAndDateFromPage equipmentNoAndDateFromPage) {

        // System.out.println(equipmentNoAndDateFromPage);
        try {
            adminReviewService.repairReviewMore(equipmentNoAndDateFromPage.getEquipmentNoAndDate());
        } catch (DateNullException e) {
            return JsonData.buildError(e.getMessage());
        }

        return JsonData.buildSuccess(0, "ok", "操作成功");
    }
}
